package com.zytlifeservice.service.find;

import com.zytlifeservice.domain.User;
import com.zytlifeservice.domain.find.Integral;
import com.zytlifeservice.domain.find.IntegralOrder;
import com.zytlifeservice.domain.find.IntegralScale;
import com.zytlifeservice.domain.find.UserInfo;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.repository.find.IntegralOrderRepository;
import com.zytlifeservice.repository.find.IntegralRepository;
import com.zytlifeservice.repository.find.UserInfoRepository;
import com.zytlifeservice.repository.pay.IntegralScaleRepository;
import com.zytlifeservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 积分业务类
 * Created by Administrator on 2017/5/31.
 */
@Service
public class IntegralService {
    @Autowired
    private IntegralRepository integralRepository;//积分持久对象

    @Autowired
    private IntegralOrderRepository integralOrderRepository;//积分商品订单对象

    @Autowired
    IntegralScaleRepository integralScaleRepository;

    @Autowired
    UserService userService;

    @Autowired
    UserInfoRepository userInfoRepository;

    /*
    添加积分
     */
//    @Transactional
    public Integer addIntegral(Integral integral, float mount) {
        List<IntegralScale> all = integralScaleRepository.findAll();
        IntegralScale scale;
//        if (all == null || all.size() == 0) {
        scale = new IntegralScale();
        scale.setMoneyToScoreScale(1);
        scale.setScoreToMoneyScale(1);
//        }
//        scale = all.get(0);
        Integer result = 0;
        try {
            integral.setIntegral((int) (mount / scale.getMoneyToScoreScale()));
            System.out.println(integral.getIntegral());
            Integer res = userService.updateToIntegral(integral.getUserId(), integral.getIntegral(), Integral.TYPE_ADD);
            if (res == 1) {
                integral.setCreateDate(new Date());

                //添加积分成功以后，接着更新统计信息。
                UserInfo userInfo = userInfoRepository.findByUserId(integral.getUserId());
                if (userInfo == null) {
                    userInfo = new UserInfo();
                    userInfo.setUserId(integral.getUserId());
                    userInfo.setTotalConsumption((double) mount);
                } else {
                    userInfo.setTotalConsumption(userInfo.getTotalConsumption() + mount);
                }
                userInfoRepository.save(userInfo);
                integralRepository.save(integral);
            }
            result = 1;
        } catch (Exception ex) {
            result = 0;
            ex.printStackTrace();
        }
        return result;
    }

    //查询单个积分类型
    public Integral findOddIntegral(Long id) {
        return integralRepository.getOne(id);
    }

    //删除积分类型
    @Transactional
    public Integer deleteIntegral(Long id) {
        Integer result = 0;
        try {
            integralRepository.delete(id);
            result = 1;
        } catch (Exception ex) {
            result = 0;
            ex.printStackTrace();
        }
        return result;
    }

    //添加兑换积分商品订单
    public Integer addIntegralOrder(IntegralOrder integralOrder) {
        Integer result = 0;
        try {
            integralOrderRepository.save(integralOrder);
            result = 1;
        } catch (Exception ex) {
            result = 0;
            ex.printStackTrace();
        }
        return result;
    }

    //查询单个积分商品订单
    public IntegralOrder findOddIntegralOrder(Long id) {
        return integralOrderRepository.getOne(id);
    }

    //发货填写快递修改订单
    @Transactional
    public Integer updateIntegralOrder(IntegralOrder integralOrder) {
        Integer result = 0;
        try {
            integralOrderRepository.save(integralOrder);
            result = 1;
        } catch (Exception ex) {
            result = 0;
            ex.printStackTrace();
        }
        return result;
    }

    //添加分页分页查询
    public Page<Integral> queryAllIntegral(int page, int size, long userId) {
        User user = userService.findOddUser(userId);
        if (user == null) {
            throw new UserException("用户不存在", 202);
        }
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = new PageRequest(page, size, sort);
//        return integralRepository.findAll(pageable);
        return integralRepository.findAllByUserId(userId, pageable);
    }
}
